package com.tenbit.tbw.ctl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.tbw.svc.EMP1010SVC;
import com.tenbit.tbw.svc.PDT1010SVC;
import com.tenbit.tbw.svc.PDT1020SVC;
import com.tenbit.tbw.vo.EMP1010VO;
import com.tenbit.tbw.vo.PDT1010VO;
import com.tobesoft.platform.PlatformRequest;
import com.tobesoft.platform.PlatformResponse;
import com.tobesoft.platform.data.ColumnInfo;
import com.tobesoft.platform.data.Dataset;
import com.tobesoft.platform.data.DatasetList;
import com.tobesoft.platform.data.VariableList;

@Controller
public class Miplatform01 {
	@Autowired
	private PDT1010SVC svc;
	
	@Autowired
	private EMP1010SVC svc1;
	
	@Autowired
	private PDT1020SVC svc2;
	
	private Logger log=Logger.getLogger(this.getClass());
	
	private Map<String,Object> paramMap= new HashMap<String,Object>();
	private Map<String,Object> resultMap = new HashMap<String,Object>();
	
	@RequestMapping("PDT1010M1.do")
	public void getProductInfoList(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		DatasetList inDatasetList = new DatasetList();
		DatasetList outDatasetList = new DatasetList();
		VariableList inVariableList = new VariableList();
		VariableList outVariableList = new VariableList();
		
		int page=1;
		int limit=100;
		
		String sellStartDate1 =null; 
		String sellStartDate2 =null; 
		String productName=null;

		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		inVariableList = inRequest.getVariableList();
		
		if(inVariableList.getValueAsString("startdate1")!=null && inVariableList.getValueAsString("startdate1")!="")
		{
			sellStartDate1= inVariableList.getValueAsString("startdate1").substring(0,4)
					+"-"+inVariableList.getValueAsString("startdate1").substring(4,6)
					+"-"+inVariableList.getValueAsString("startdate1").substring(6,8);
		}
		if(inVariableList.getValueAsString("startdate2")!=null && inVariableList.getValueAsString("startdate2")!="")
		{
		sellStartDate2= inVariableList.getValueAsString("startdate2").substring(0,4)
				+"-"+inVariableList.getValueAsString("startdate2").substring(4,6)
				+"-"+inVariableList.getValueAsString("startdate2").substring(6,8);
		}
		
		productName=inVariableList.getValueAsString("productname");
		
		int startrow = (page-1)*10+1;
		int endrow=startrow+100-1;
		
		log.debug("sellStartDate1:"+sellStartDate1);
		log.debug("sellStartDate2:"+sellStartDate2);
		
		
		paramMap.put("sellStartDate1", sellStartDate1);
		paramMap.put("sellStartDate2", sellStartDate2);
		paramMap.put("productName",productName);
		paramMap.put("page",page);
		paramMap.put("startrow",startrow);
		paramMap.put("endrow", endrow);
		
		int listcount=0;
		
		resultMap=svc.getProductInfoList(paramMap);
		
		listcount=(Integer) svc.getProductInfoListCount(paramMap).get("listCount");
		
		
		Dataset ds=new Dataset("list_data","utf-8");
		
		try
		{
			
			if(resultMap.get("productInfoList")!=null)
			{
				
				List<PDT1010VO> list = (List<PDT1010VO>) resultMap.get("productInfoList");
				
				log.debug(list);
				
				ds.addColumn("product_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("product_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("product_num",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("item_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("item_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("company_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("category_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("product_size",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("unit_masure",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("unit_price",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("employee_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("sell_start_date",ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds.addColumn("sell_stop_date",ColumnInfo.COLUMN_TYPE_STRING, 20);
				
				for(int i = 0; i<list.size() ; i++)
				{
					int row = ds.appendRow();
					
					ds.setColumn(row, "product_code", list.get(i).getProductCode());
					ds.setColumn(row, "product_name", list.get(i).getProductName());
					ds.setColumn(row, "product_num", list.get(i).getProductNumber());
					ds.setColumn(row, "item_code", list.get(i).getItemCode());
					ds.setColumn(row, "item_name", list.get(i).getItemName());
					ds.setColumn(row, "company_name", list.get(i).getCompanyName());
					ds.setColumn(row, "category_name",list.get(i).getCategoryName());
					ds.setColumn(row, "product_size", list.get(i).getProductSize());
					ds.setColumn(row, "unit_masure", list.get(i).getUnitMasure());
					ds.setColumn(row, "unit_price",list.get(i).getUnitPrice().toString());
					ds.setColumn(row, "employee_name", list.get(i).getEmployeeName());
					
					String[] seq1 = (String[]) ((String) list.get(i).getSellStartDate()).split("-");
					String[] seq2 = (String[]) ((String) list.get(i).getSellStopDate()).split("-");
					
					if(seq1[1].length()<2){
						seq1[1]="0"+seq1[1];
					}
					if(seq1[2].length()<2){
						seq1[2]="0"+seq1[2];
					}
					
					if(seq2[1].length()<2){
						seq2[1]="0"+seq2[1];
					}
					if(seq2[2].length()<2){
						seq2[2]="0"+seq2[2];
					}
					
					sellStartDate1=seq1[0]+seq1[1]+seq1[2];
					sellStartDate2=seq2[0]+seq2[1]+seq2[2];
					
					
					ds.setColumn(row, "sell_start_date", sellStartDate1);
					ds.setColumn(row, "sell_stop_date", sellStartDate2);
				}
				outDatasetList.add("list_data",ds);				
				outVariableList.addStr("ErrorCode", "0");
				outVariableList.addStr("ErrorMsg", "SUCC");
			}
		} catch (Exception e)
		{
			outVariableList.addStr("ErrorCode", "-1");
			outVariableList.addStr("ErrorMsg", e.getMessage());	
		}
		
		
		int maxpage = (int)((double)listcount/limit+0.95);
		int startpage =(((int)((double)page / 10+0.9))-1)*10+1;
		
		
		
		String[] seq1 = (String[]) ((String) resultMap.get("sellStartDate1")).split("-");
		String[] seq2 = (String[]) ((String) resultMap.get("sellStartDate2")).split("-");
		
		if(seq1[1].length()<2){
			seq1[1]="0"+seq1[1];
		}
		if(seq1[2].length()<2){
			seq1[2]="0"+seq1[2];
		}
		
		if(seq2[1].length()<2){
			seq2[1]="0"+seq2[1];
		}
		if(seq2[2].length()<2){
			seq2[2]="0"+seq2[2];
		}
		
		sellStartDate1=seq1[0]+seq1[1]+seq1[2];
		sellStartDate2=seq2[0]+seq2[1]+seq2[2];
		
		outVariableList.addStr("startdate1",sellStartDate1);
		outVariableList.addStr("startdate2",sellStartDate2);
		outVariableList.addStr("productname",productName);
		
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		outResponse.sendData(outVariableList, outDatasetList);
	}
	
	@RequestMapping("codebook.do")
	public void getCodeBookIngo(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		VariableList outVariableList = new VariableList();
		DatasetList outDatasetList = new DatasetList();
		
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		Dataset ds_item=new Dataset("ds_item","utf-8");
		Dataset ds_company=new Dataset("ds_company","utf-8");
		Dataset ds_category=new Dataset("ds_category","utf-8");
		Dataset ds_measure=new Dataset("ds_measure","utf-8");
		
		List<EMP1010VO> list=svc1.getEmployeeCodebook();
		
		ds_item.addColumn("item_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
		ds_item.addColumn("item_code_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
		
		ds_measure.addColumn("measure_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
		ds_measure.addColumn("measure_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
		
		ds_company.addColumn("company_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
		ds_company.addColumn("company_code_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
		
	
		
		ds_category.addColumn("category_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
		ds_category.addColumn("category_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
		
		List<Map<String,String>> category = svc.getCategoryInfo();
		
		log.debug(category);
		
		for(int i=0;i<list.size();i++)
		{
			
			if(list.get(i).getCodeType().equals("ITEM")&&!list.get(i).getCodeDetail().equals("ITEM"))
			{
				int row = ds_item.appendRow();
				
				ds_item.setColumn(row, "item_code", list.get(i).getCodeDetail());
				ds_item.setColumn(row, "item_code_name", list.get(i).getCodeName());
				
			} else if(list.get(i).getCodeType().equals("COMPANY")&&!list.get(i).getCodeDetail().equals("COMPANY"))
			{
				int row = ds_company.appendRow();
				
				ds_company.setColumn(row, "company_code", list.get(i).getCodeDetail());
				ds_company.setColumn(row, "company_code_name", list.get(i).getCodeName());
				
			} else if(list.get(i).getCodeType().equals("MEASURE")&&!list.get(i).getCodeDetail().equals("MEASURE"))
			{
				int row = ds_measure.appendRow();
				
				ds_measure.setColumn(row, "measure_code", list.get(i).getCodeDetail());
				ds_measure.setColumn(row, "measure_name", list.get(i).getCodeName());
			}
		}
		
		for(int i=0; i<category.size();i++)
		{
			int row = ds_category.appendRow();
			
			ds_category.setColumn(row, "category_code", category.get(i).get("CODE"));
			ds_category.setColumn(row, "category_name", category.get(i).get("NAME"));
		}
		
		log.debug("ds_item:"+ds_item);
		log.debug("ds_company:"+ds_company);
		log.debug("ds_category:"+ds_category);
		log.debug("ds_measure:"+ds_measure);
		
		outDatasetList.addDataset(ds_item);
		outDatasetList.addDataset(ds_company);	
		outDatasetList.addDataset(ds_category);
		outDatasetList.addDataset(ds_measure);
		
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		outResponse.sendData(outVariableList,outDatasetList);
	}
	
	
	@RequestMapping("PDT1020P1.do")
	public void getProductInfoDetail(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		log.debug(" ==============PDT1010P1.do-getProductInfoDetail: 상품상세정보 목록 ==============");

		VariableList outVariableList = new VariableList();
		VariableList inVariableList = new VariableList();
		DatasetList outDatasetList = new DatasetList();
		
		PDT1010VO vo=new PDT1010VO();
		PDT1010VO productInfo=new PDT1010VO();
		
		
		
		int productNum=0;
		
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		Dataset ds_detail_info=new Dataset("ds_detail_info","utf-8");
		Dataset ds_file=new Dataset("ds_file","utf-8");
		
		if(inRequest!=null)
		{
			inVariableList = inRequest.getVariableList();
			if(inVariableList!=null)
			{
				if(inVariableList.getValueAsInteger("productNum")!=null)
				{
					productNum=inVariableList.getValueAsInteger("productNum");
					vo.setProductNumber(productNum);
					resultMap=svc2.getProductInfoDetail(vo);
					productInfo=(PDT1010VO) resultMap.get("productDetailInfo");
					
					ds_detail_info.addColumn("emp_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("emp_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("emp_dept",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("pdt_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("pdt_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("item_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("cate_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("company_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("pdt_no",ColumnInfo.COLUMN_TYPE_INT, 20);
					ds_detail_info.addColumn("unit_size",ColumnInfo.COLUMN_TYPE_STRING, 200);
					ds_detail_info.addColumn("measure_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("unit_price",ColumnInfo.COLUMN_TYPE_CURRENCY, 100);
					
					ds_file.addColumn("file_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_file.addColumn("file",ColumnInfo.COLUMN_TYPE_BLOB, 256);
					
					int row = ds_detail_info.appendRow();
					int row1= ds_file.appendRow();
					
					ds_detail_info.setColumn(row, "emp_code",productInfo.getEmployeeCode());
					ds_detail_info.setColumn(row, "emp_name",productInfo.getEmployeeName());
					ds_detail_info.setColumn(row, "emp_dept",productInfo.getDepartmentName());
					ds_detail_info.setColumn(row, "pdt_code",productInfo.getProductCode());
					ds_detail_info.setColumn(row, "pdt_name",productInfo.getProductName());
					ds_detail_info.setColumn(row, "item_code",productInfo.getItemCode());
					ds_detail_info.setColumn(row, "cate_name",productInfo.getCategoryName());
					ds_detail_info.setColumn(row, "company_code",productInfo.getCompanyCode());
					ds_detail_info.setColumn(row, "pdt_no",productInfo.getProductNumber());
					ds_detail_info.setColumn(row, "unit_size",productInfo.getProductSize());
					ds_detail_info.setColumn(row, "measure_code",productInfo.getUnitMeasureCode());
					ds_detail_info.setColumn(row, "unit_price",productInfo.getUnitPrice().intValue());
					
					ds_file.setColumn(row1, "file_name",productInfo.getAttachedFileName());
					
					
				} else{
					productNum=10046;
					vo.setProductNumber(productNum);
					resultMap=svc2.getProductInfoDetail(vo);
					productInfo=(PDT1010VO) resultMap.get("productDetailInfo");
					
					ds_detail_info.addColumn("emp_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("emp_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("emp_dept",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("pdt_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("pdt_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("item_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("cate_name",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("company_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("pdt_no",ColumnInfo.COLUMN_TYPE_INT, 20);
					ds_detail_info.addColumn("unit_size",ColumnInfo.COLUMN_TYPE_STRING, 200);
					ds_detail_info.addColumn("measure_code",ColumnInfo.COLUMN_TYPE_STRING, 20);
					ds_detail_info.addColumn("unit_price",ColumnInfo.COLUMN_TYPE_CURRENCY, 100);
					
					ds_file.addColumn("file",ColumnInfo.COLUMN_TYPE_BLOB, 256);
					ds_file.addColumn("file_name",ColumnInfo.COLUMN_TYPE_STRING, 100);
					
					int row = ds_detail_info.appendRow();
					int row1= ds_file.appendRow();
					
					ds_detail_info.setColumn(row, "emp_code",productInfo.getEmployeeCode());
					ds_detail_info.setColumn(row, "emp_name",productInfo.getEmployeeName());
					ds_detail_info.setColumn(row, "emp_dept",productInfo.getDepartmentName());
					ds_detail_info.setColumn(row, "item_code",productInfo.getItemCode());
					ds_detail_info.setColumn(row, "cate_name",productInfo.getCategoryName());
					ds_detail_info.setColumn(row, "company_code",productInfo.getCompanyCode());
					ds_detail_info.setColumn(row, "measure_code",productInfo.getUnitMeasureCode());
					
					ds_file.setColumn(row1, "file_name","");
				}
			}
		} 
		
		outDatasetList.addDataset(ds_detail_info);
		outDatasetList.addDataset(ds_file);
		
		log.debug(ds_detail_info);
		
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		outResponse.sendData(outVariableList,outDatasetList);
	}
	
	@RequestMapping("PDT1021P1.do")
	public void getProductInfoInput(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		VariableList inVariableList = new VariableList();
		VariableList outVariableList = new VariableList();
		DatasetList inDatasetList = new DatasetList();
		DatasetList outDatasetList = new DatasetList();
		
		PDT1010VO vo=new PDT1010VO();
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		inDatasetList = inRequest.getDatasetList();
		
		inVariableList = inRequest.getVariableList();
		
		String mode=inVariableList.getValueAsString("mode");
		
		
		Dataset ds_detail_info=inDatasetList.getDataset("ds_detail_info");
		
		Dataset ds_file=inDatasetList.getDataset("ds_file");
		
		vo=dataSetToBean(ds_detail_info);
		
		vo.setAttachedFileName(ds_file.getColumnAsString(0, "file_name"));
		
		if(mode.equals("I")){
			
			resultMap=svc2.insProductInfoDetail(vo);
		} else if(mode.equals("M")){
			vo.setProductNumber(ds_detail_info.getColumnAsInteger(0, "pdt_no"));
			resultMap=svc2.setProductInfoDetail(vo);
		}
		
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		
		if(!(Boolean) resultMap.get("result")){
			outVariableList.add("ErrorCode", -1);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			
			outResponse.sendData(outVariableList,outDatasetList);
		} else {
			
			upload(ds_file);
			
			log.debug((String)resultMap.get("msg"));
			
			outVariableList.add("ErrorCode", 0);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			
			outResponse.sendData(outVariableList,outDatasetList);
		}
			
	}
	@RequestMapping("PDT1022P1.do")
	public void delProductInfo(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		VariableList inVariableList = new VariableList();
		VariableList outVariableList = new VariableList();
		DatasetList inDatasetList = new DatasetList();
		DatasetList outDatasetList = new DatasetList();
		
		PDT1010VO vo=new PDT1010VO();
		
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		inDatasetList = inRequest.getDatasetList();
		
		Dataset ds_detail_info=inDatasetList.getDataset("ds_detail_info");
		
		vo.setProductNumber(ds_detail_info.getColumnAsInteger(0, "pdt_no"));
		
		resultMap=svc2.delProductInfoDetail(vo);
		
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		
		if(!(Boolean) resultMap.get("result")){
			outVariableList.add("ErrorCode", -1);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			
			outResponse.sendData(outVariableList,outDatasetList);
		} else {
			
			outVariableList.add("ErrorCode", 0);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			
			outResponse.sendData(outVariableList,outDatasetList);
		}
	}
	private PDT1010VO dataSetToBean(Dataset ds) {
		
		PDT1010VO vo=new PDT1010VO();
		
		vo.setEmployeeCode(ds.getColumnAsInteger(0, "emp_code"));
		vo.setEmployeeName(ds.getColumnAsString(0, "emp_name"));
		vo.setProductCode(ds.getColumnAsString(0,"pdt_code"));
		vo.setProductName(ds.getColumnAsString(0, "pdt_name"));
		vo.setItemCode(ds.getColumnAsString(0, "item_code"));
		vo.setCategoryName(ds.getColumnAsString(0,"cate_name"));
		vo.setCompanyCode(ds.getColumnAsString(0, "company_code"));
		
		vo.setProductSize(ds.getColumnAsString(0, "unit_size"));
		vo.setUnitMeasureCode(ds.getColumnAsString(0, "measure_code"));
		vo.setUnitPrice(ds.getColumnAsCurrency(0, "unit_price"));
	
		return vo;
	}
	
	public void upload(Dataset ds)
	{
		  StringBuffer name=new StringBuffer(); 
		  
		  name.append("C:\\TBWORKSHOP\\workspace\\tbworkshop\\WebContent\\upload\\");  
		  name.append(ds.getColumnAsString(0, "file_name"));

		  byte[] file=ds.getColumnAsByteArray(0, "file");
		  
		  if(file!=null){
			  
			  FileOutputStream fileOut = null;
			  BufferedOutputStream bufferedOut = null;
			  
			  try 
			  {
				  
				  fileOut = new FileOutputStream(new File(name.toString()));
				  bufferedOut=new BufferedOutputStream(fileOut);
				  bufferedOut.write(file);
				  bufferedOut.flush();
				  bufferedOut.close(); 
				  fileOut.close();
				  
			  } catch (Exception e) {
				  
				  e.printStackTrace();
			  }
			  
		  }
		  
		 
	}
	
}
